import re


def split_cmd(content):
    rs = []
    tmp = content
    while len(tmp) > 0:
        exp = re.findall(r'\{\{.*\}\}', tmp)
        if exp:
            rs.extend(re.split(r'\s+|\t+|\n+', tmp[0:tmp.find(exp[0])]))
            rs.append(exp[0])
            tmp = tmp.split(exp[0])[-1]
        else:
            rs.extend(re.split(r'\s+', tmp))
            tmp = ''
    rs = list(filter(lambda x: x, rs))
    jar_elements = [elem for elem in rs if elem.endswith(".jar")]
    py_elements = [elem for elem in rs if elem.endswith(".py")]
    result = rs
    if jar_elements:
        # 找到第一个以".jar"结尾的元素
        jar_element = jar_elements[-1]
        # 找到该元素的索引
        index_of_element = rs.index(jar_element)
        # 将该元素后面的所有元素拼接成一个字符串
        result = rs[0:index_of_element]
        args = ' '.join(rs[index_of_element + 1:])
        if not args.strip() == '':
            application = jar_element + ' ' + args
        else:
            application = jar_element
        result.append(application)
    elif py_elements:
        # 找到第一个以".jar"结尾的元素
        py_element = py_elements[-1]
        # 找到该元素的索引
        index_of_element = rs.index(py_element)
        # 将该元素后面的所有元素拼接成一个字符串
        result = rs[0:index_of_element]
        args = ' '.join(rs[index_of_element + 1:])
        if not args.strip() == '':
            application = py_element + ' ' + args
        else:
            application = py_element
        result.append(application)
    return result


if __name__ == '__main__':
    result = split_cmd('--driver-memory 1g\n--num-executors 8\n--executor-memory 20g\n--executor-cores 1\npatent_fw_cn_para.jar\n--basic_parquet_path cosn://data-datalake-pd-io-dataprod-ash-1251949819/deltalake/pd_dw.db/dws_patent_basic/\n--claim_parquet_path cosn://data-datalake-pd-io-dataprod-ash-1251949819/deltalake/pd_dw.db/dws_patent_claim/\n--offset_parquet_path cosn://data-datalake-pd-io-dataprod-ash-1251949819/deltalake/pd_dw.db/dws_patent_claim_offset/\n--description_parquet_path cosn://data-datalake-pd-io-dataprod-ash-1251949819/deltalake/pd_dw.db/dws_patent_description/\n--related_parquet_path cosn://data-dw-process-dataprod-ash-1251949819/llm/patent/file_wrapper/cn_related_compare_patent/\n--output_path cosn://testpatsnapus-1251949819/patent2/file_wrapper/cn_para_text/\n--repartition_num 10')
    print(result)

